﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace 随机数抽奖工具
{
    public partial class MainForm : Form
    {
        private Point mPoint;
        private Random random;
        private bool flag;
        public int Num { get; set; }

        public MainForm()
        {
            InitializeComponent();
            random = new Random();
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        private void panel1_Click(object sender, System.EventArgs e)
        {
            Thread.Sleep(50);
            Environment.Exit(0);
        }

        private void panel3_Click(object sender, EventArgs e)
        {
            if (!flag)
            {
                flag = !flag;
                panel3.BackgroundImage = Properties.Resources.stop;
                new Thread(StartRun).Start();
            }
            else
            {
                flag = !flag;
                panel3.BackgroundImage = Properties.Resources.start;
                //不包含300，请修改该值
                label3.InvokeText(Convert.ToString(random.Next(1, Num)));
            }
        }

        private void StartRun()
        {
            while (flag)
            {
                label3.InvokeText(Convert.ToString(random.Next(1, Num)));
                Thread.Sleep(10);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //显示输入框
            NumInputForm form = new NumInputForm();
            DialogResult result = form.ShowDialog();
            if (result != DialogResult.OK)
            {
                Environment.Exit(0);
            }
            else
            {
                Num = form.Num;
            }
        }
    }
}
